package com.boang.controller;

import com.boang.po.Cla;
import org.aopalliance.intercept.Interceptor;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/*
  @author: 13813
  @date: 2024/7/25 15:32
*/
@Controller
@RequestMapping("/cla")
//@RestController
public class ClaController {

    @RequestMapping("/getClaList")
    public String getClaList(String name, ModelMap modelMap, Model model) {
        modelMap.put("name", "张三");
        model.addAttribute("age", 20);
        return "cla";
    }

    @RequestMapping("/getClaById")
    public ModelAndView getClaById(int id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("name", "张三");
        modelAndView.addObject("age", 20);
        modelAndView.setViewName("cla");
        return modelAndView;
    }

    @RequestMapping("/getClaByName")
    public String getClaByName(@RequestParam("claName") String name) {
        System.out.println(name);
        return "cla";
    }

    @RequestMapping("/addCla")
    public String addCla(Cla cla) {
        System.out.println(cla);
        return "cla";
    }

    @RequestMapping("/testJson") //@RequestBody 把json转对象
    @ResponseBody //把对象转json
    public Cla testJson(@RequestBody Cla cla) {
        cla.setName("AAA");
        return cla;
    }

    @RequestMapping("/testException")
    public String testJson() {
        int i = 1 / 0;
        return "";
    }

    //http://localhost:8080/cla/1/51/33
    //http://localhost:8080/springmvc_war_exploded/cla/testRest?id=1&name=51&num=33
    @PostMapping("/testRest/{id}/{name}/{num}")
    public String testRestPost(@PathVariable("id") int id, @PathVariable("name") String name, @PathVariable("num") int num) {
        System.out.println("id = " + id);
        System.out.println("name = " + name);
        System.out.println("num = " + num);
        return "";
    }

    @GetMapping("/testRest/{id}")
    public String testRestGet(@PathVariable("id") int id) {
        System.out.println("id = " + id);
        return "";
    }

    @RequestMapping("/up")
    public String up(MultipartFile file) throws IOException {
        //上传文件的名称
        String originalFilename = file.getOriginalFilename();
        //获取上传文件流
        InputStream inputStream = file.getInputStream();
        Files.copy(inputStream, Paths.get("E:\\upload\\" + originalFilename));
        return "success";
    }

    @RequestMapping("/download")
    public ResponseEntity<byte[]> download() throws Exception {
        //要下载的文件
        File file = new File("E:\\upload\\web51周测试成绩表24.7.25.xls");
        //http头信息
        HttpHeaders headers = new HttpHeaders();
        //下载的文件名称，设置编码，防止中文乱码
        String downloadFileName = new String("web51周测试成绩表24.7.25.xls".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        //设置下载的头
        headers.setContentDispositionFormData("attachment", downloadFileName);
        //以流的方式写数据到客户端
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
    }
}
